package com.ssy.lingxi.order.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.UUID;

/**
 * 字符串工具类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-08-07
 */
public class OrderStringUtil {
    /**
     * Json序列化对象
     * @param obj 对象
     * @return json字符串
     */
    public static String serializeObject(Object obj) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(obj);
        } catch (Exception ignored) {
            return "";
        }
    }

    /**
     * Json反序列化对象
     *
     * @param json   Json字符串
     * @param tClass 反序列化对象的Class
     * @return 对象
     */
    public static <T> T deserializeObject(String json, Class<T> tClass) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return mapper.readValue(json, tClass);
        } catch (Exception ignored) {
            return null;
        }
    }

    /**
     * 屏蔽字符串
     *
     * @param plainString 字符串
     * @return 屏蔽字符串
     */
    public static String coverString(String plainString) {
        if (!StringUtils.hasText(plainString)) {
            return "";
        }

        final String maskString = "*";
        switch (plainString.length()) {
            case 1:
                return plainString;
            case 2:
                return plainString.substring(0, 1) + maskString;
            case 3:
                return plainString.substring(0, 1) + maskString + plainString.substring(2);
            case 4:
                return plainString.substring(0, 1) + String.join("",Collections.nCopies(2, maskString)) + plainString.substring(2);
            case 5:
            case 6:
                return plainString.substring(0, 2) + String.join("",Collections.nCopies(2, maskString)) + plainString.substring(4);
            default:
                return plainString.substring(0, 3) + String.join("",Collections.nCopies(3, maskString)) + plainString.substring(6);
        }
    }

    /**
     * 生成支付订单号
     * @param orderNo 订单号
     * @return 字符串
     */
    public static String randomPayOrderNo(String orderNo) {
        return orderNo.concat("-").concat(RandomStringUtils.random(7, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"));
    }

    /**
     * 生成支付订单号
     * @return 字符串
     */
    public static String randomPayOrderNo() {
        return "MP".concat("-").concat(RandomStringUtils.random(10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"));
    }

    /**
     * 生成UUID字符串，用于缓存支付信息
     * @return UUID字符串
     */
    public static String randomUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 生成随机（重复概率非常低）的订单号
     * @return 订单号
     */
    public static String randomUniqueNumber() {
        return "D".concat(RandomStringUtils.random(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"));
    }

    /**
     * 随机（重复概率非常低）的字符串
     * @param prefix 前缀
     * @return 字符串
     */
    public static String randomUniqueNumber(String prefix) {
        return prefix.concat(RandomStringUtils.random(7, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"));
    }

    /**
     * 生成随机8位的自提码
     * @return 自提码
     */
    public static String randomSelfCodeNumber() {
        return RandomStringUtils.random(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
    }
}
